package com.example.lenovo.weiyi;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.com.ruanmeng.demon.CollectionM;
import com.com.ruanmeng.utils.Params;
import com.com.ruanmeng.utils.PreferencesUtils;
import com.yolanda.nohttp.NoHttp;
import com.yolanda.nohttp.rest.Request;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import adapter.BaseLoadMoreHeaderAdapter;
import butterknife.BindView;
import butterknife.ButterKnife;
import nohttp.CallServer;
import nohttp.CustomHttpListener;
import share.Const;
import share.HttpIP;

/**
 * 我的收藏
 */
public class MineCollectionActivity extends BaseActivity {

    @BindView(R.id.rb_healthy)
    RadioButton rbHealthy;
    @BindView(R.id.rb_luntan)
    RadioButton rbLuntan;
    @BindView(R.id.rb_colection)
    RadioGroup rbColection;
    @BindView(R.id.rv_collection)
    RecyclerView rvCollection;
    @BindView(R.id.tv_collection_null)
    TextView tvCollectionNull;
    @BindView(R.id.rl_colection)
    SwipeRefreshLayout mRefresh;
    @BindView(R.id.rv_lt)
    RecyclerView rvLt;
    private LinearLayout li_null;
    private LinearLayoutManager linearLayoutManager;
    ArrayList<CollectionM.RowsBean> Temp_list = new ArrayList<>();
    int page = 1;
    private MyHealthyCollectionAdapter mAdapter;
    private LinearLayoutManager linearLayoutManager1;
    private MyLtCollectionAdapter mltAdapter;
    private String type = "1";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_mine_collection);
        ButterKnife.bind(this);
        ChangLayTitle("我的收藏");
        LayBack();
        init();
        Params.Change_Collection="";
        getData();
    }

    @Override
    protected void onResume() {
        super.onResume();
        if (!TextUtils.isEmpty(Params.Change_Collection)) {
            Params.Change_Collection = "";
            page = 1;
            getData();
        }
    }
    public void init() {
        mRefresh.setColorSchemeResources(R.color.MainColor);
        mRefresh.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                page = 1;
                getData();
            }
        });
        //健康知识
        linearLayoutManager = new LinearLayoutManager(this);
        rvCollection.setLayoutManager(linearLayoutManager);
        mAdapter = new MyHealthyCollectionAdapter(MineCollectionActivity.this, rvCollection, Temp_list, R.layout.item_text);
        rvCollection.setAdapter(mAdapter);
        mAdapter.setOnItemClickListener(new BaseLoadMoreHeaderAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View view, int position) {
                Intent in = new Intent(MineCollectionActivity.this, HealthMessXQActivity.class);
                in.putExtra("id", Temp_list.get(position + 1).getCourseId());
                startActivity(in);
            }
        });
        mAdapter.setOnLoadMoreListener(new BaseLoadMoreHeaderAdapter.OnLoadMoreListener() {
            @Override
            public void onLoadMore() {
                page++;
                getData();
            }
        });
        //论坛
        linearLayoutManager1 = new LinearLayoutManager(this);
        rvLt.setLayoutManager(linearLayoutManager1);
        mltAdapter = new MyLtCollectionAdapter(MineCollectionActivity.this, rvLt, Temp_list, R.layout.item_collectionlt);
        rvLt.setAdapter(mltAdapter);
        mltAdapter.setOnItemClickListener(new BaseLoadMoreHeaderAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View view, int position) {
                Intent in = new Intent(MineCollectionActivity.this, LunTanXQActivity.class);
                in.putExtra("id", Temp_list.get(position + 1).getQuestionId());
                in.putExtra("collection", "1");
                startActivity(in);
            }
        });
        mltAdapter.setOnLoadMoreListener(new BaseLoadMoreHeaderAdapter.OnLoadMoreListener() {
            @Override
            public void onLoadMore() {
                page++;
                getData();
            }
        });
    }

    private void getData() {
        Request<String> mRequest = NoHttp.createStringRequest(HttpIP.MineCollection, Const.POST);
        mRequest.addHeader("token", PreferencesUtils.getString(MineCollectionActivity.this, "uid"));
        mRequest.add("page", page);
        mRequest.add("type", type);
        CallServer.getRequestInstance().add(MineCollectionActivity.this, 0, mRequest,
                new CustomHttpListener(MineCollectionActivity.this, true, CollectionM.class) {

                    @Override
                    public void doWork(Object data, boolean isOne) {
                        CollectionM model = ((CollectionM) data);
                        if (page == 1) {
                            Temp_list.clear();
                        }
                        if (("1").equals(type)) {//健康知识
                            rvLt.setVisibility(View.GONE);
                            Temp_list.addAll(model.getRows());
                            if (Temp_list.size() == 0) {
                                tvCollectionNull.setVisibility(View.VISIBLE);
                                rvCollection.setVisibility(View.GONE);
                            } else {
                                tvCollectionNull.setVisibility(View.GONE);
                                rvCollection.setVisibility(View.VISIBLE);
                            }
                            mAdapter.notifyDataSetChanged();
                        } else {
                            rvCollection.setVisibility(View.GONE);
                            Temp_list.addAll(model.getRows());
                            if (Temp_list.size() == 0) {
                                tvCollectionNull.setVisibility(View.VISIBLE);
                                rvLt.setVisibility(View.GONE);
                            } else {
                                tvCollectionNull.setVisibility(View.GONE);
                                rvLt.setVisibility(View.VISIBLE);
                            }
                            mltAdapter.notifyDataSetChanged();
                        }
                    }

                    @Override
                    public void onFinally(JSONObject obj) throws JSONException {
                        super.onFinally(obj);
                        mRefresh.setRefreshing(false);
                    }
                }, true, true);
    }

    /**
     * 论坛收藏
     */
    public class MyLtCollectionAdapter extends BaseLoadMoreHeaderAdapter<CollectionM.RowsBean> {


        public MyLtCollectionAdapter(Context mContext, RecyclerView recyclerView, List<CollectionM.RowsBean> mDatas, int mLayoutId) {
            super(mContext, recyclerView, mDatas, mLayoutId);
        }

        @Override
        public void convert(Context mContext, RecyclerView.ViewHolder holder, CollectionM.RowsBean s) {
            TextView tv_title = (TextView) holder.itemView.findViewById(R.id.tv_collection_tilte);
            TextView tv_collection_content = (TextView) holder.itemView.findViewById(R.id.tv_collection_content);
            View view_ban = (View) holder.itemView.findViewById(R.id.view_collection_ban);
            View view_man = (View) holder.itemView.findViewById(R.id.view_collection_man);
            tv_title.setText(s.getTitle());
            tv_collection_content.setText(s.getContent());
            if (holder.getLayoutPosition() == Temp_list.size() - 1) {
                view_ban.setVisibility(View.GONE);
                view_man.setVisibility(View.VISIBLE);
            } else {
                view_ban.setVisibility(View.VISIBLE);
                view_man.setVisibility(View.GONE);
            }
        }
    }

    /**
     * 健康知识回复
     */
    public class MyHealthyCollectionAdapter extends BaseLoadMoreHeaderAdapter<CollectionM.RowsBean> {


        public MyHealthyCollectionAdapter(Context mContext, RecyclerView recyclerView, ArrayList<CollectionM.RowsBean> mDatas, int mLayoutId) {
            super(mContext, recyclerView, mDatas, mLayoutId);
        }

        @Override
        public void convert(Context mContext, RecyclerView.ViewHolder holder, CollectionM.RowsBean s) {
            TextView tv_title = (TextView) holder.itemView.findViewById(R.id.tv_text_tilte);
            View view_ban = (View) holder.itemView.findViewById(R.id.view_text_ban);
            View view_man = (View) holder.itemView.findViewById(R.id.view_text_man);
            tv_title.setText(s.getTitle());
            if (holder.getLayoutPosition() == Temp_list.size() - 1) {
                view_ban.setVisibility(View.GONE);
                view_man.setVisibility(View.VISIBLE);
            } else {
                view_ban.setVisibility(View.VISIBLE);
                view_man.setVisibility(View.GONE);
            }
        }
    }

    public void doClick(View v) {
        Intent in;
        switch (v.getId()) {
            case R.id.rb_healthy://	回复状态0：未回复，1已回复
                type = "1";
                page = 1;
                getData();
                break;
            case R.id.rb_luntan:
                type = "2";
                page = 1;
                getData();
                break;
        }
    }
}
